/******************************************************
 *                                                     
 *              Score System               
 *                                                     
 *******************************************************/

/**
 * ScoreServlet.java
 *
 * 网页的响应Servlet
 *
 * @author sundy
 * @version 0.0.1
 * lastModified 2011-01-01
 */

package com.sundy.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sundy.core.Common;
import com.sundy.core.Context;
import com.sundy.core.Utility;
import com.sundy.daosupport.DescriptTblDAO;
import com.sundy.daosupport.StudentTblDAO;
import com.sundy.daosupport.Subject2descDAO;
import com.sundy.daosupport.SubjectTblDAO;
import com.sundy.service.DescriptTbl;
import com.sundy.service.StudentTbl;
import com.sundy.service.Subject2desc;
import com.sundy.service.SubjectTbl;
import com.sundy.tools.WebUtility;

public class ScoreServlet extends AbstractServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3787380199270577696L;

	public ScoreServlet() {
	}

	protected void doGet(Context context, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(context, request, response);
	}

	protected void doPost(Context context, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		if (id == null || id.trim().equals("")) {
			Utility.showInfor(request, response, "参数错误！");
			return;
		}
		String sub = request.getParameter("sub");
		if (sub == null || sub.trim().equals("")) {
			Utility.showInfor(request, response, "参数错误！");
			return;
		}
		DescriptTblDAO dao = DescriptTblDAO
				.getFromApplicationContext(Common.applicationContext);
		DescriptTbl item = dao.findById(Integer.valueOf(id));
		if (item == null) {
			Utility.showError(request, response,
					"奇怪，未找到该登分表，也许已经删除了，或许还没有创建，谁知道呢？！");
			return;
		}
		if (item.getFinished() != null && item.getFinished() == 1) {
			Utility.showInfor(request, response, "该登分表已经完成，不能再编辑了！");
			return;
		}
		int subjectid = Integer.valueOf(sub);
		if (!WebUtility.scoreIt(context.getCurrentUser(), subjectid)) {
			Utility.showInfor(request, response, "您不能使用该登分表，请与管理员联系！");
			return;
		}
		boolean isView = false;
		Subject2descDAO sddao = Subject2descDAO
				.getFromApplicationContext(Common.applicationContext);
		List<Subject2desc> sds = sddao.findBySubDesc(subjectid, Integer
				.valueOf(id));
		if (sds != null && !sds.isEmpty()) {
			Subject2desc s2d = sds.get(0);
			if (s2d.getFinished() != null && s2d.getFinished() == 1) {
				isView = true;
			}
		}
		StudentTblDAO sdao = StudentTblDAO
				.getFromApplicationContext(Common.applicationContext);
		List<StudentTbl> list = sdao.findByDescId(item.getId());
		SubjectTblDAO sjdao = SubjectTblDAO
				.getFromApplicationContext(Common.applicationContext);
		SubjectTbl subject = sjdao.findById(subjectid);
		request.setAttribute("isview", isView);
		request.setAttribute("item", item);
		request.setAttribute("lst", list);
		request.setAttribute("subject", subject);
		request.setAttribute("max", WebUtility.getSubjectMax(subjectid));
		Utility.showJSP(request, response, "/score.jsp");
	}
}
